package com.qgs.rabbitmqapi.returnListener;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author: 蔡翔
 * @Date: 2019/7/19 13:26
 * @Version 1.0
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.0.229");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPort(5672);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        String exchange = "test_return_exchange";
        String routingKey = "return.#";
        String queueName = "test_return_queue";
        channel.queueDeclare(queueName,true,false,false,null);
        channel.exchangeDeclare(exchange,"topic",true);
        channel.queueBind(queueName,exchange,routingKey);

        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String msg = new String(body,"UTF-8");
                System.out.println("客戶端接收到的msg是："+msg);
            }
        };

        channel.basicConsume(queueName,true,defaultConsumer);
    }
}
